﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SdmxMl.GenericMetadata;
using SdmxMl.UC;

namespace SdmxMl.Panels
{
    public partial class DlgSearchReportedAttrib : Form
    {
        private List<AttributeValueSetType> aList;
        private MetadataSetType metaSet;
        private ReportedAttributeType reportedAttribute;
        private DataTable tbResult;

        public DlgSearchReportedAttrib(MetadataSetType mSet, ReportedAttributeType repAttribute)
        {
            InitializeComponent();
            metaSet = mSet;
            reportedAttribute = repAttribute;
            aList = new List<AttributeValueSetType>(16);
            buttonNavigate.Enabled = false;
        }


        private void textBoxQuickSearch_KeyDown(object sender, KeyEventArgs e)
        {

            if ((e.KeyData & (Keys.Shift | Keys.Alt)) == 0)
            {
                switch (e.KeyData & Keys.KeyCode)
                {
                    case Keys.Return:
                        if ((e.KeyData & Keys.Control) == 0)
                        {
                            buttonSearch_Click(this, EventArgs.Empty);
                        }
                        break;
                }
            }
        }

        private DataTable CreateTable()
        {
            DataTable tb = new DataTable("Resluts");
            tb.Columns.Add(new DataColumn("Keys", typeof(string)));
            tb.Columns.Add(new DataColumn("Text", typeof(string)));

            return tb;
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            tbResult = CreateTable();
            dataGridViewResult.DataSource = null;
            buttonNavigate.Enabled = false;

            string pattern = textBoxContent.Text;
            
            SearchType st = Search.DefinePattern(ref pattern);
            if (pattern.Length > 0)
            {
                foreach (AttributeValueSetType att in metaSet.AttributeValueSetList)
                {
                    string text = string.Empty;
                    ReportedAttributeType r = att.ReportedAttributeList.GetReportedAttributeOfConcept(reportedAttribute.ConceptID);
                    if (r != null)
                        text = r.Value.ToString();
                    if (text.Length > 0)
                    {
                        if (Search.MatchPattern(st, pattern, text))
                        {
                            aList.Add(att);
                            tbResult.Rows.Add(new object[] { att.ToString(), text });
                        }
                    }

                }

                dataGridViewResult.DataSource = tbResult;
                buttonNavigate.Enabled = true;
            }

        }

        private void dataGridViewResult_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            buttonNavigate_Click(this, EventArgs.Empty);
        }

        private DataRow FindSelectedRow()
        {
            DataRow findRow = null;
            if (dataGridViewResult.DataSource != null)
            {
                BindingManagerBase bm = dataGridViewResult.BindingContext[dataGridViewResult.DataSource, dataGridViewResult.DataMember];
                if (bm.Count > 0 && bm.Current != null)
                    findRow = ((DataRowView)bm.Current).Row;
            }

            return findRow;
        }

        private void buttonNavigate_Click(object sender, EventArgs e)
        {
            DataRow r = FindSelectedRow();
            if (r != null)
            {
                int index = tbResult.Rows.IndexOf(r);
                AttributeValueSetType att = aList[index];
                TreeNode targetNode = null;
                TreeView tv = SdmxMl.Manager.ReferenceManager.TreeSdmx;
                SdmxMl.Helper.TreeHelper.LocateObject(tv, att, ref targetNode);

                if (targetNode != null)
                {
                    tv.BeginUpdate();
                    tv.SelectedNode = targetNode;
                    tv.SelectedNode.EnsureVisible();
                    tv.EndUpdate();
                    tv.Focus();
                }
            }
        }

    }
}
